<template>
    <div class="login_container">
        <div class="login_box">
            <div class="toux_box">
                <img src="../assets/logo.png" alt="">
            </div>
            <el-form ref="formRef" :rules="rules" :model="form" label-width="0px" class="login_form">
                <el-form-item prop="username">
                    <el-input v-model="form.username" placeholder="用户名" prefix-icon="el-icon-user-solid"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="form.password" type="password" placeholder="密码" prefix-icon="el-icon-lock"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="login('formRef')" :loading="isLoading">登录</el-button>
                    <el-button type="info" @click="resetForm('formRef')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
export default {
  data () {
    return {
      isLoading: false,
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login (refName) {
      this.isLoading = true
      this.$refs[refName].validate(async vaild => {
        if (!vaild) return false
        try {
          console.log(this.isLoading)
          var { data: resp } = await this.$http.post('/login', this.form)
          if (resp.meta.status === 200) {
            this.$message({
              message: '恭喜你，登陆成功',
              type: 'success'
            })
            window.sessionStorage.setItem('token', resp.data.token)
            this.$router.push('/home')
          } else {
            this.$message({
              message: resp.meta.msg,
              type: 'error'
            })
          }
        } catch (err) {
          console.dir('err.message')
          this.$message({
            message: '网络错误，请联系管理员',
            type: 'error'
          })
        }
      })
      this.isLoading = false
    },
    resetForm (refName) {
      this.$refs[refName].resetFields()
    }
  }
}
</script>
<style lang="less" scoped>
.login_container {
    height: 100%;
    background-color: #2b4b6b;
}

.login_box {
    width: 450px;
    height: 300px;
    border-radius: 3px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
}

.toux_box {
    width: 120px;
    height: 120px;
    padding: 10px;
    border-radius: 50%;
    border: 1px solid #eee;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: #fff;
    box-shadow: 0 0 10px #eee;
    img {
        width: 100%;
        border-radius: 50%;
        background-color: #eee;
    }
}

.login_form {
    position: absolute;
    bottom: 20px;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
    .btns {
        float: right;
    }
}
</style>
